﻿using MangoMes.Commont.DapperHelps;
using MangoMes.Commont.DTOs;
using MangoMes.Write.API.Applications.warehousess;
using MediatR;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using static MangoMes.Write.API.Applications.warehousess.warehouseCreateHandler;
using System;
using MangoMes.Commont.Domain.Jocker;
using MangoMes.Write.API.Applications.Login;
using MangoMes.Write.API.Applications.process;
using System.Diagnostics;
using ManagoMes.Read.API.Applications.Enterprises;
using MangoMes.Commont.Domain;
using MangoMes.Write.API.Applications.Enterprises;
using ManagoMes.Read.API.Applications.Departmentes;
using MangoMes.Commont.Domain.Department;
using MangoMes.Write.API.Applications.Departmentes;
using MangoMes.Write.API.Applications.CraftRoutes;
using MangoMes.Write.API.Applications.Clientss;
using MangoMes.Commont.Domain.Client;
using MangoMes.Write.API.Applications.PurchaseStorags;
using MangoMes.Commont.Domain.Purchase;
using ManagoMes.Read.API.Applications.ProductionScheduling;
using MangoMes.Write.API.Applications.Transfer;
using MangoMes.Commont.DTOs.CommontFiles;
using MangoMes.Write.API.Applications.ProductionWorkOrders;
using NPOI.HSSF.UserModel;
using NPOI.SS.UserModel;
using NPOI.XSSF.UserModel;
using System.Globalization;
using MangoMes.Commont.Domain.Products;
using MangoMes.Write.API.Applications.step;

namespace MangoMes.Write.API.Controllers
{
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class JockerController : ControllerBase
    {

        private readonly IMediator _mediator;

        public JockerController(IMediator mediator
            )
        {
            _mediator = mediator;
        }


        /// <summary>
        /// 仓库添加
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<ResultsDTO> warehouseCreate(warehouseCreateCommond request, CancellationToken cancellationToken)
        {

            try
            {
                var list = await _mediator.Send(request);

                return list;

            }
            catch (Exception)
            {

                throw;
            }

        }
        /// <summary>
        /// 日志添加
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>

        [HttpPost]
        public async Task<ResultsDTO> LoginCreate(LoginCreateCommond request, CancellationToken cancellationToken)
        {
            try
            {
                var list =await _mediator.Send(request);
                
                return list;

            }
            catch (Exception)
            {

                throw;
            }






        }
        /// <summary>
        /// 工序添加
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<ResultsDTO> processCreate(processCreateCommond request, CancellationToken cancellationToken)
        {

            try
            {
                var list = await _mediator.Send(request);

                return list;
            }
            catch (Exception)
            {

                throw;
            }
           

        }
        /// <summary>
        /// 新增企业管理
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<int> EnterpriseAdd(EnterpriseAddCommond request, CancellationToken cancellationToken)
        {
            try
            {
                var list = await _mediator.Send(request);

                return list;
            }
            catch (Exception)
            {

                throw;
            }
        }
        /// <summary>
        /// 企业列表
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<List<Enterprise>> EnterpriseList(EnterpriseListCommond request, CancellationToken cancellationToken)
        {
            try
            {
                var list = await _mediator.Send(request);

                return list;
            }
            catch (Exception)
            {

                throw;
            }
        }
        /// <summary>
        /// 部门添加
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        [HttpPost]
        public async Task<int> DepartmentsAdd(DepartmentsAddCommond request, CancellationToken cancellationToken)
        {
            try
            {
                var list = await _mediator.Send(request);

                return list;
            }
            catch (Exception)
            {

                throw;
            }
        }
        /// <summary>
        /// 部门列表
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<PageDTO<Departments>> Departmentslist(DepartmentslistCommond request, CancellationToken cancellationToken)
        {
            try
            {
                var list = await _mediator.Send(request);

                return list;
            }
            catch (Exception)
            {

                throw;
            }
        }
        /// <summary>
        /// 工艺路线新增
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<int> Handle(CraftRouteAddCommand request, CancellationToken cancellationToken)
        {
            try
            {
                var list = await _mediator.Send(request);

                return list;
            }
            catch (Exception)
            {

                throw;
            }
        }
        /// <summary>
        /// 工艺路线列表
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<PageDTO<CraftRoute>> CraftRouteList(CraftRouteListCommand request, CancellationToken cancellationToken)
        {
            try
            {
                var list = await _mediator.Send(request);

                return list;
            }
            catch (Exception)
            {

                throw;
            }
        }
        /// <summary>
        /// 编辑工艺路线
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<ResultsDTO> CraftRouteUpdate(CraftRouteUpdateCommand request, CancellationToken cancellationToken)
        {
            var list=await _mediator.Send(request);
            return list;
        }
        /// <summary>
        /// 工艺路线信息
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<ResultsDTO<CraftRoute>> CraftRouteBack(CraftRouteBackCommand request, CancellationToken cancellationToken)
        {
            var list = await _mediator.Send(request);


            return list;
        }
        /// <summary>
        /// 客户管理新增
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<ResultsDTO> ClientManagementAdd(ClientManagementAddCommand request)
        {
            var list = await _mediator.Send(request);
            return list;
        }
        /// <summary>
        /// 客户查询
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        [HttpPost]
        public async Task<PageDTO<ClientManagementDTO>> ClientManagementList(ClientManagementListCommand request, CancellationToken cancellationToken)
        {
            var list = await _mediator.Send(request);
            return list;
        }
        /// <summary>
        /// 客户类型列表
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<ResultsDTO<ClientType>> ClientTypeAdd(ClientTypeAddCommand request, CancellationToken cancellationToken)
        {
            var list = await _mediator.Send(request);
            return list;
        }
        /// <summary>
        /// 客户管理获取信息
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<ResultsDTO<ClientManagement>> ClientManagementBack(ClientManagementBackCommand request, CancellationToken cancellationToken)
        {
            var list = await _mediator.Send(request);
            return list;
        }
        /// <summary>
        /// 客户类型修改
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<ResultsDTO> ClientManagementUpdate(ClientManagementUpdateCommand request, CancellationToken cancellationToken)
        {
            var list = await _mediator.Send(request);
            return list;
        }
        /// <summary>
        /// 客户管理删除
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<ResultsDTO> ClientManagementDelete(ClientManagementDeleteCommand request, CancellationToken cancellationToken)
        {
            var list = await _mediator.Send(request);
            return list;
        }
            /// <summary>
            /// 工序修改
            /// </summary>
            /// <param name="request"></param>
            /// <param name="cancellationToken"></param>
            /// <returns></returns>
            [HttpPost]
        public async Task<ResultsDTO> processUpdate(processUpdateCommond request, CancellationToken cancellationToken)
        {
            try
            {
                var list = await _mediator.Send(request);

                return list;
            }
            catch (Exception)
            {

                throw;
            }

        }
        /// <summary>
        /// 采购入库添加
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<ResultsDTO> PurchaseStorageAdd(PurchaseStorageAddCommand request)
        {
            var list = await _mediator.Send(request);

            return list;
        }
        /// <summary>
        /// 采购入库列表
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<PageDTO<PurchaseStorageDTO>> PurchaseStorageList(PurchaseStorageListCommand request, CancellationToken cancellationToken)
        {
            var list = await _mediator.Send(request);

            return list;
        }
        /// <summary>
        /// 采购入库单反填信息
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<ResultsDTO<PurchaseStorageDTO>> PurchaseStorageBack(PurchaseStorageBackCommand request, CancellationToken cancellationToken)
        {
            var list = await _mediator.Send(request);

            return list;
        }
        /// <summary>
        /// 采购入库单修改
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<ResultsDTO> PurchaseStorageUpdate(PurchaseStorageUpdateCommand request, CancellationToken cancellationToken)
        {
            var list = await _mediator.Send(request);

            return list;
        }
        /// <summary>
        /// 执行入库
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<ResultsDTO> Execute(ExecuteCommand request, CancellationToken cancellationToken)
        {
            var list = await _mediator.Send(request);

            return list;
        }
        /// <summary>
        /// 物料添加
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<ResultsDTO> MaterialAdd(MaterialAddCommand request, CancellationToken cancellationToken)
        {
            var list = await _mediator.Send(request);

            return list;
        }
        /// <summary>
        /// 物料列表
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<PageDTO<MaterialDTO>> MaterialList(MaterialListCommand request, CancellationToken cancellationToken)
        {
            var list = await _mediator.Send(request);

            return list;
        }
        /// <summary>
        /// 客户管理导入
        /// </summary>
        /// <param name="file"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<ResultsDTO> ImportProduction(IFormFile file)
        {
            ResultsDTO res = new ResultsDTO();
            var exName = Path.GetExtension(file.FileName);
            if (exName == ".xls" || exName == ".xlsx")
            {

            }
            else
            {
                res.Message = "传输的文件格式有误";
                return res;
            }
            using (var ms = new MemoryStream())
            {

                file.CopyTo(ms);
                ms.Position = 0;
                IWorkbook wb = null;
                if (exName == ".xls")//创建87-2003
                {
                    wb = new HSSFWorkbook(ms);
                }
                else
                {
                    wb = new XSSFWorkbook(ms);
                }
                ISheet sheet = wb.GetSheetAt(0);
                int rowNum = sheet.LastRowNum;
                int cellNum = sheet.GetRow(0).LastCellNum;
                ClientManagementAddCommand order = new ClientManagementAddCommand();
                order.ClientId = 0;
                for (int i = 1; i <= rowNum; i++)
                {
                    var a = sheet.GetRow(i).GetCell(4).ToString();
                    order.ClientNo = sheet.GetRow(i).GetCell(0).ToString();
                    order.ClientName = sheet.GetRow(i).GetCell(1).ToString();
                    order.ClientAName = sheet.GetRow(i).GetCell(2).ToString();
                    order.ClientEname = sheet.GetRow(i).GetCell(3).ToString();
                    order.ClientTypeId = Convert.ToInt32(sheet.GetRow(i).GetCell(4).ToString());
                    order.ClientSynopsis = sheet.GetRow(i).GetCell(5).ToString();
                    order.ClientSite = sheet.GetRow(i).GetCell(6).ToString();
                    order.ClientWebsite = sheet.GetRow(i).GetCell(7).ToString();
                    order.ClientEmail = sheet.GetRow(i).GetCell(8).ToString();
                    order.ClientPhone = sheet.GetRow(i).GetCell(9).ToString();
                    order.ClientLogo = sheet.GetRow(i).GetCell(10).ToString();
                    order.ClientContact1 = sheet.GetRow(i).GetCell(11).ToString();
                    order.ClientPhone1 = sheet.GetRow(i).GetCell(12).ToString();
                    order.ClientEmail1 = sheet.GetRow(i).GetCell(13).ToString();
                    order.ClientContact2 = sheet.GetRow(i).GetCell(14).ToString();
                    order.ClientPhone2 = sheet.GetRow(i).GetCell(15).ToString();
                    order.ClientEmail2 = sheet.GetRow(i).GetCell(16).ToString();
                    order.SocialCreditCode = sheet.GetRow(i).GetCell(17).ToString();
                    order.Remark = sheet.GetRow(i).GetCell(18).ToString();
                    order.IsNo = 0;
                    res = await ClientManagementAdd(order);
                }
                return res;
            }
        }
        /// <summary>
        /// 物料列表下拉框
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<ResultsDTO<ProductManage>> ProductList(ProductListCommand request, CancellationToken cancellationToken)
        {
            var list = await _mediator.Send(request);

            return list;
        }
        /// <summary>
        /// 仓库列表下拉框
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<ResultsDTO<Stashs>> warehouseList(warehouseListCommand request, CancellationToken cancellationToken)
        {
            var list = await _mediator.Send(request);
            return list;
        
        }
        /// <summary>
        /// 采购入库导入
        /// </summary>
        /// <param name="file"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<ResultsDTO> ImportPurchaseStorage(IFormFile file)
        {
            ResultsDTO res = new ResultsDTO();
            var exName = Path.GetExtension(file.FileName);
            if (exName == ".xls" || exName == ".xlsx")
            {

            }
            else
            {
                res.Message = "传输的文件格式有误";
                return res;
            }
            using (var ms = new MemoryStream())
            {

                file.CopyTo(ms);
                ms.Position = 0;
                IWorkbook wb = null;
                if (exName == ".xls")//创建87-2003
                {
                    wb = new HSSFWorkbook(ms);
                }
                else
                {
                    wb = new XSSFWorkbook(ms);
                }
                ISheet sheet = wb.GetSheetAt(0);
                int rowNum = sheet.LastRowNum;
                int cellNum = sheet.GetRow(0).LastCellNum;
                PurchaseStorageAddCommand order = new PurchaseStorageAddCommand();
                order.PurchaseId = 0;
                for (int i = 1; i <= rowNum; i++)
                {
                    
                    order.PurchaseNo = sheet.GetRow(i).GetCell(0).ToString();
                    order.PurchaseName = sheet.GetRow(i).GetCell(1).ToString();
                    var a = sheet.GetRow(i).GetCell(2).ToString();
                    string[] formats = { "dd-M月-yyyy", "yyyy-MM-dd" };
                    DateTime thisDate;
                    foreach (var format in formats)
                    {
                        if (DateTime.TryParseExact(a, new[] { format }, CultureInfo.InvariantCulture, DateTimeStyles.None, out thisDate))
                        {
                            order.PurchaseDate = Convert.ToDateTime(thisDate.ToString("yyyy-MM-dd"));
                            break;
                        }
                    }
                   

                    order.ArrivalNoticeId = Convert.ToInt32(sheet.GetRow(i).GetCell(3).ToString());
                    order.PurchaseIndentId = Convert.ToInt32(sheet.GetRow(i).GetCell(4).ToString());
                    order.SupplierId = Convert.ToInt32(sheet.GetRow(i).GetCell(5).ToString());
                    order.ReceiptState = sheet.GetRow(i).GetCell(6).ToString();
                    order.Remark = sheet.GetRow(i).GetCell(7).ToString();
                    order.IsNo = 0;
                    res = await PurchaseStorageAdd(order);
                }
                return res;
            }
        }
        /// <summary>
        /// 生产任务添加
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<ResultsDTO> ProductionCreate(ProductionCreateCommond request, CancellationToken cancellationToken)
        {

            try
            {
                var list = await _mediator.Send(request);
              
                return list;
            }
            catch (Exception)
            {

                throw;
            }

        }


        /// <summary>
        /// 生产任务修改
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<ResultsDTO<givebirth>> SchedulingDisplayUpdate(SchedulingDisplayUpdateCommond request, CancellationToken cancellationToken)
        {

            try
            {
                var list = await _mediator.Send(request);
                return list;
            }
            catch (Exception)
            {

                throw;
            }


        }


        /// <summary>
        /// 转移挑拨单添加
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<ResultsDTO> TransferCreate(TransferCreateCommod request, CancellationToken cancellationToken)
        {
            try
            {
                var list = await _mediator.Send(request);

                return list;
            }
            catch (Exception)
            {

                throw;
            }


        }

        /// <summary>
        /// 工序步骤
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<ResultsDTO> StepCreat(StepCreatCommond request, CancellationToken cancellationToken)
        {

            try
            {
                var list =await _mediator.Send(request);
                return list;
            }
            catch (Exception)
            {

                throw;
            }
            


        }
        /// <summary>
        /// 工序步骤修改
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<ResultsDTO> StepUpdate(StepUpdateCommond request, CancellationToken cancellationToken)
        {

            try
            {
                var list = await _mediator.Send(request);

                return list;
            }
            catch (Exception)
            {

                throw;
            }
            

        }
    }


}
